package com.roncoo.pay.permission.service.impl;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.roncoo.pay.common.core.page.PageBean;
import com.roncoo.pay.common.core.page.PageParam;
import com.roncoo.pay.permission.entity.PmsPermission;
import com.roncoo.pay.permission.entity.PmsRolePermission;
import com.roncoo.pay.permission.repository.PmsPermissionRepository;
import com.roncoo.pay.permission.repository.PmsRolePermissionRepository;
import com.roncoo.pay.permission.service.PmsRolePermissionService;
import com.roncoo.pay.permission.service.PmsUserRoleService;

/**
 * 角色权限service接口实现
 *
 */
@Service("pmsRolePermissionService")
public class PmsRolePermissionServiceImpl implements PmsRolePermissionService {
	@Autowired
	private PmsRolePermissionRepository pmsRolePermissionRepository;
	@Autowired
	private PmsPermissionRepository pmsPermissionRepository;
	@Autowired
	private PmsUserRoleService pmsOperatorRoleService;

	/**
	 * 根据操作员ID，获取所有的功能权限集
	 * 
	 * @param userId
	 */
	public Set<String> getPermissionsByOperatorId(String userId) {
		// 根据操作员Id查询出关联的所有角色id
		String roleIds = pmsOperatorRoleService.getRoleIdsByOperatorId(userId);

		String permissionIds = getActionIdsByRoleIds(roleIds);
		Set<String> permissionSet = new HashSet<String>();

		// 根据角色ID字符串得到该用户的所有权限拼成的字符串
		if (!StringUtils.isEmpty(permissionIds)) {
			List<String> ids = Arrays.asList(permissionIds.split(","));
			
			for(String id : ids){
				PmsPermission pmsPermission = pmsPermissionRepository.findOne(id);
				if(null != pmsPermission){
					permissionSet.add(pmsPermission.getPermission());	
				}
			}
		}
		return permissionSet;
	}

	/**
	 * 根据角色ID集得到所有权限ID集
	 * 
	 * @param roleIds
	 * @return actionIds
	 */
	private String getActionIdsByRoleIds(String roleIds) {
		StringBuilder actionIdsBuf = new StringBuilder("");
		// 得到角色－权限表中roleiId在ids中的所有关联对象
		String[] ids = roleIds.split(",");
		for(String id : ids){
			if(StringUtils.isNotEmpty(id)){
				List<PmsRolePermission> listRolePermission = pmsRolePermissionRepository.findByRoleId(id);
				// 拼接字符串
				for (PmsRolePermission pmsRolePermission : listRolePermission) {
					actionIdsBuf.append(pmsRolePermission.getPermissionId()).append(",");
				}
			}
		}
		
		String actionIds = actionIdsBuf.toString();
		// 截取字符串
		if (StringUtils.isEmpty(actionIds) && actionIds.length() > 0) {
			actionIds = actionIds.substring(0, actionIds.length() - 1); // 去掉最后一个逗号
		}
		return actionIds;
	}

	// /////////////////////////////下面：基本操作方法///////////////////////////////////////////////

	/**
	 * 创建pmsOperator
	 */
	public void saveData(PmsRolePermission pmsRolePermission) {
		pmsRolePermissionRepository.save(pmsRolePermission);
	}

	/**
	 * 修改pmsOperator
	 */
	public void updateData(PmsRolePermission pmsRolePermission) {
		pmsRolePermissionRepository.save(pmsRolePermission);
	}

	/**
	 * 根据id获取数据pmsOperator
	 * 
	 * @param id
	 * @return
	 */
	public PmsRolePermission getDataById(String id) {
		return pmsRolePermissionRepository.findOne(id);

	}

	/**
	 * 分页查询pmsOperator
	 * 
	 * @param pageParam
	 * @param ActivityVo
	 *            PmsOperator
	 * @return
	 */
	public PageBean<PmsRolePermission> listPage(PageParam pageParam, PmsRolePermission pmsRolePermission) {
		Example<PmsRolePermission> example = Example.of(pmsRolePermission);
		
		Pageable pg = new PageRequest(pageParam.getPageNum(), pageParam.getNumPerPage());
		
		Page<PmsRolePermission> page = pmsRolePermissionRepository.findAll(example, pg);
		
		PageBean<PmsRolePermission> pb = new PageBean<PmsRolePermission>(pageParam.getPageNum(), pageParam.getNumPerPage(), page.getTotalPages(),page.getContent());
		return pb;
	}
	
	/**
	 * 保存角色和权限之间的关联关系
	 */
	public void saveRolePermission(String roleId, String rolePermissionStr){
		// 删除原来的角色与权限关联
		pmsRolePermissionRepository.deleteByRoleId(roleId);
		if (!StringUtils.isEmpty(rolePermissionStr)) {
			// 创建新的关联
			String[] permissionIds = rolePermissionStr.split(",");
			for (int i = 0; i < permissionIds.length; i++) {
				String permissionId = permissionIds[i];
				PmsRolePermission item = new PmsRolePermission();
				item.setPermissionId(permissionId);
				item.setRoleId(roleId);
				pmsRolePermissionRepository.save(item);
			}
		}
	}
}
